/**
 * Copyright (C), 2020, 安徽贝慕信息科技有限公司
 * FileName: DealerAppControllerImpl
 * Author:   Allen
 * Date:     2020/8/27 17:28
 * Description: 商家应用接入号登记处理实现类
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.bim.marvel.rest.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bim.marvel.openapi.client.dto.DealerAppDTO;
import com.bim.marvel.openapi.client.query.DealerAppQuery;
import com.bim.marvel.openapi.client.query.PageQuery;
import com.bim.marvel.common.core.constant.CodeEnum;
import com.bim.marvel.common.core.model.dto.SimplePageVO;
import com.bim.marvel.common.core.model.dto.SimpleVO;
import com.bim.marvel.common.util.SimpleConverter;
import com.bim.marvel.openapi.core.entity.DealerAppDO;
import com.bim.marvel.openapi.core.service.impl.DealerAppService;
import com.bim.marvel.rest.DealerAppController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 〈商家应用接入号登记处理实现类〉
 *
 * @author Allen
 * @date 2020/8/27
 * @since 1.0.0
 */
@RestController
public class DealerAppControllerImpl implements DealerAppController {

    /**
     * 注入DealerAppService
     */
    @Autowired
    private DealerAppService dealerAppService;

    @Override
    public SimpleVO<Boolean> saveDealerApp(DealerAppQuery dealerAppQuery) {
        SimpleVO<Boolean> simpleVO = new SimpleVO(CodeEnum.SUCCESS);
        boolean success = dealerAppService.saveDealerApp(dealerAppQuery);
        if (!success) {
            simpleVO = new SimpleVO(CodeEnum.INSERT_FAILED);
        }
        return simpleVO;
    }

    @Override
    public SimpleVO<Boolean> reviewDealerApp(String appId) {
        SimpleVO<Boolean> simpleVO = new SimpleVO(CodeEnum.SUCCESS);
        boolean success = dealerAppService.reviewDealerApp(appId, true);
        if (!success) {
            simpleVO = new SimpleVO(CodeEnum.UPDATE_FAILED);
        }
        return simpleVO;
    }

    @Override
    public SimpleVO<List<DealerAppDTO>> listDealerAppByCompanyCode(String companyCode) {
        List<DealerAppDO> dealerAppDOList = dealerAppService.listDealerApp(companyCode);
        if (CollectionUtils.isEmpty(dealerAppDOList)) {
            return new SimpleVO<>(CodeEnum.DATA_NOT_FOUND);
        }
        List<DealerAppDTO> dealerAppDTOList = SimpleConverter.convert(dealerAppDOList,
                DealerAppDTO.class);
        return new SimpleVO<>(dealerAppDTOList);
    }

    @Override
    public SimplePageVO<List<DealerAppDTO>> listDealerApp(String companyCode, PageQuery pageQuery) {
        Page page = new Page(pageQuery.getPageIndex(), pageQuery.getPageSize());
        DealerAppDO dealerAppDO = new DealerAppDO();
        dealerAppDO.setCompanyCode(companyCode);
        QueryWrapper<DealerAppDO> queryWrapper = new QueryWrapper<>(dealerAppDO);
        IPage<DealerAppDO> dealerAppDOIPage = dealerAppService.page(page, queryWrapper);
        List<DealerAppDO> dealerAppDOList = dealerAppDOIPage.getRecords();
        if (CollectionUtils.isEmpty(dealerAppDOList)) {
            return new SimplePageVO<>(CodeEnum.DATA_NOT_FOUND);
        }
        List<DealerAppDTO> dealerAppDTOList = SimpleConverter.convert(dealerAppDOList, DealerAppDTO.class);
        return new SimplePageVO(dealerAppDTOList, dealerAppDOIPage);
    }
}
